package com.inspectortime.webapp.inspector.fee;

import javax.servlet.http.HttpServletRequest;

public final class FeeUtils {

	private static final String FEE_WIZARD_ADVANCED_KEY = "feeWizardAdvancedHelper";

	private static final String FEE_WIZARD_BASIC_FLAT_FEE_KEY = "feeWizardBasicFlatFeeHelper";
	private static final String FEE_WIZARD_BED_BATH_KEY = "feeWizardBedBathHelper";
	private static final String FEE_WIZARD_LISTING_PRICE_KEY = "feeWizardListingPriceHelper";
	private static final String FEE_WIZARD_SQFT_KEY = "feeWizardSqFtHelper";
	
	private static void clearFeeWizardHelpersFromSession(
			HttpServletRequest request) {
		request.getSession().setAttribute(FEE_WIZARD_LISTING_PRICE_KEY, null);
		request.getSession().setAttribute(FEE_WIZARD_BED_BATH_KEY, null);
		request.getSession().setAttribute(FEE_WIZARD_SQFT_KEY, null);
		request.getSession().setAttribute(FEE_WIZARD_ADVANCED_KEY, null);
		request.getSession().setAttribute(FEE_WIZARD_BASIC_FLAT_FEE_KEY, null);
	}

	
	
	public static FeeWizardListingPriceHelper retrieveAdvancedFeeWizardHelperFromSession(
			HttpServletRequest request) {
		FeeWizardListingPriceHelper helper = (FeeWizardListingPriceHelper) request
				.getSession().getAttribute(FEE_WIZARD_ADVANCED_KEY);
		return helper;
	}
	
	public static FeeWizardBasicFlatFeePlanHelper retrieveBasicFlatFeeFeeWizardHelperFromSession(
			HttpServletRequest request) {
		FeeWizardBasicFlatFeePlanHelper helper = (FeeWizardBasicFlatFeePlanHelper) request
				.getSession().getAttribute(FEE_WIZARD_BASIC_FLAT_FEE_KEY);
		return helper;
	}
	
	public static FeeWizardBedBathHelper retrieveBedBathFeeWizardHelperFromSession(
			HttpServletRequest request) {
		FeeWizardBedBathHelper helper = (FeeWizardBedBathHelper) request
				.getSession().getAttribute(FEE_WIZARD_BED_BATH_KEY);
		return helper;
	}
	
	public static FeeWizardListingPriceHelper retrieveListingPriceFeeWizardHelperFromSession(
			HttpServletRequest request) {
		FeeWizardListingPriceHelper helper = (FeeWizardListingPriceHelper) request
				.getSession().getAttribute(FEE_WIZARD_LISTING_PRICE_KEY);
		return helper;
	}
	
	public static FeeWizardSqFtHelper retrieveSqFtFeeWizardHelperFromSession(
			HttpServletRequest request) {
		FeeWizardSqFtHelper helper = (FeeWizardSqFtHelper) request.getSession()
				.getAttribute(FEE_WIZARD_SQFT_KEY);
		return helper;
	}
	
	public static FeeWizardCommonHelper retrieveWizardCommonHelperFromSession(
			HttpServletRequest request) {
		FeeWizardCommonHelper helper = (FeeWizardCommonHelper) request
				.getSession().getAttribute(FEE_WIZARD_BED_BATH_KEY);
		if (helper != null) {
			return helper;
		}
		helper = (FeeWizardCommonHelper) request.getSession().getAttribute(
				FEE_WIZARD_LISTING_PRICE_KEY);
		if (helper != null) {
			return helper;
		}
		helper = (FeeWizardCommonHelper) request.getSession().getAttribute(
				FEE_WIZARD_SQFT_KEY);
		if (helper != null) {
			return helper;
		}
		helper = (FeeWizardCommonHelper) request.getSession().getAttribute(
				FEE_WIZARD_BASIC_FLAT_FEE_KEY);
		if (helper != null) {
			return helper;
		}
		helper = (FeeWizardCommonHelper) request.getSession().getAttribute(
				FEE_WIZARD_ADVANCED_KEY);
		if (helper != null) {
			return helper;
		}
		throw new RuntimeException("Expected to find helper in session");
	}
	
	public static void storeAdvancedFeeWizardInSession(HttpServletRequest request, FeeWizardCommonHelper helper) {
		clearFeeWizardHelpersFromSession(request);
		request.getSession().setAttribute(FEE_WIZARD_ADVANCED_KEY, helper);
	}
	
	public static void storeBasicFlatFeeFeeWizardInSession(HttpServletRequest request, FeeWizardCommonHelper helper) {
		clearFeeWizardHelpersFromSession(request);
		request.getSession().setAttribute(FEE_WIZARD_BASIC_FLAT_FEE_KEY, helper);
	}

	public static void storeBedBathFeeWizardInSession(HttpServletRequest request, FeeWizardCommonHelper helper) {
		clearFeeWizardHelpersFromSession(request);
		request.getSession().setAttribute(FEE_WIZARD_BED_BATH_KEY, helper);
	}

	public static void storeListingPriceFeeWizardInSession(HttpServletRequest request, FeeWizardCommonHelper helper) {
		clearFeeWizardHelpersFromSession(request);
		request.getSession().setAttribute(FEE_WIZARD_LISTING_PRICE_KEY, helper);
	}

	public static void storeSqFtFeeWizardInSession(HttpServletRequest request, FeeWizardCommonHelper helper) {
		clearFeeWizardHelpersFromSession(request);
		request.getSession().setAttribute(FEE_WIZARD_SQFT_KEY, helper);
	}

	private FeeUtils() {
	}

}
